package cat.joronya.supermanager.service.substrImpl;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Locale;

import cat.joronya.supermanager.Supermanager;
import cat.joronya.supermanager.service.MyTeamsParser;
import cat.joronya.supermanager.team.Team;

public class MyTeamsParserImpl implements MyTeamsParser
{
	public ArrayList<Team> teams;
	
	// start position
	public static final String START_POSITION_STRING = "background=\"gif/tusequipos2.gif\"";
	public static final String STOP_POSITION_STRING = "background=\"gif/tusequiposabajo2.gif\"";
	
	// start tr
	public static final String START_TR = "<tr>";
	public static final String STOP_TR = "</tr>";
	
	// start colspan
	public static final String START_COLSPAN = "colspan=\"6\"";
	
	// start colspan
	public static final String START_CODIGO = "style=\"text-align:right;\">Código</td>";
	
	// start jornada
	public static final String START_DAY = "Jornada ";
	public static final String STOP_DAY = "</td>";
	
	// start idequ
	public static final String START_IDEQU = "class=\"grisizqda\" style=\"text-align:right;\">";
	public static final String STOP_IDEQU = "&nbsp;&nbsp;</td>";
	
	// start name
	public static final String START_NAME = "<a href=\"verequipo.php?id_equ=";
	public static final String START_NAME2 = "\">";
	public static final String STOP_NAME = "</a> ";
	
	// start lesionats
	public static final String START_LESIONATS = "<img src=\"gif/lesion.gif\" width=\"15\" height=\"15\" border=\"0\" alt=\"";
	public static final String STOP_LESIONATS = " ";
	
	// start baixa
	public static final String START_BAIXA = "gif/baja.gif";
	
	// start equip incomplet
	public static final String START_INCOMPLETE = "<font color=\"red\">(";
	public static final String STOP_INCOMPLETE = " jug.)</font>";
	
	// start jornada rating and position
	public static final String START_DAY_RATING = "class=\"gris\">";
	public static final String STOP_DAY_RATING = " (";
	public static final String STOP_DAY_POSITION = "º)</td>";
	
	// start general rating and postion
	public static final String START_GENERAL_RATING = "class=\"gris\">";
	public static final String STOP_GENERAL_RATING = " (";
	public static final String STOP_GENERAL_POSITION = "º)</td>";
	
	// start broker rating and postion
	public static final String START_BROKER_RATING = "class=\"gris\">";
	public static final String STOP_BROKER_RATING = "  (";
	public static final String STOP_BROKER_POSITION = "º)</td>";

	public long parse(String html) throws Exception
	{
		long start = System.nanoTime();
		
		NumberFormat nf = NumberFormat.getInstance(new Locale("es","ES"));
		
		teams = new ArrayList<Team>();
		
		int startPosition = html.indexOf(START_POSITION_STRING);
		if( startPosition == -1 )
			throw new Exception();
		
		int stopPosition = html.indexOf(STOP_POSITION_STRING);
		
		// jornada
		int day = 0;
		
		// recorrem els tr, comencem pel 3er tr
		int startTr, stopTr;
		startTr = html.indexOf(START_TR, startPosition);
		//startTr = html.indexOf(START_TR, startTr+START_TR.length());
		//startTr = html.indexOf(START_TR, startTr+START_TR.length());
		while( startTr != -1 && startTr < stopPosition )
		{
			// recuperem tr
			stopTr = html.indexOf(STOP_TR, startTr);
			String tr = html.substring(startTr, stopTr);
			
			// si hi ha colspan=6, fila no valida
			if( tr.indexOf(START_COLSPAN) != -1 )
			{
				// recuperem seguent startTr
				startTr = html.indexOf(START_TR, stopTr);
				
				continue;
			}
			
			// si fila headers, agafem jornada i continuem
			if( tr.indexOf(START_CODIGO) != -1 )
			{
				// agafem jornada
				int startDay = tr.indexOf(START_DAY);
				int stopDay = tr.indexOf(STOP_DAY, startDay+START_DAY.length());
				String dayString = tr.substring(startDay+START_DAY.length(), stopDay);
				day = Integer.valueOf(dayString);
				
				// recuperem seguent startTr
				startTr = html.indexOf(START_TR, stopTr);
				
				continue;
			}
			
			Team team = new Team();
			team.setDay(day);
			
			// idequ
			int startIdequ = tr.indexOf(START_IDEQU);
			int stopIdequ = tr.indexOf(STOP_IDEQU, startIdequ+START_IDEQU.length());
			String idequ = tr.substring(startIdequ+START_IDEQU.length(), stopIdequ);
			team.setIdEqu(idequ);
			
			// name
			int startName = tr.indexOf(START_NAME+idequ+START_NAME2, stopIdequ+STOP_IDEQU.length());
			int stopName = tr.indexOf(STOP_NAME, startName+(START_NAME+idequ+START_NAME2).length());
			String name = tr.substring(startName+(START_NAME+idequ+START_NAME2).length(), stopName);
			team.setName(name);
			
			// lesionats
			int startLesionats = tr.indexOf(START_LESIONATS, stopName+STOP_NAME.length());
			if( startLesionats != -1 )
			{
				int stopLesionats = tr.indexOf(STOP_LESIONATS, startLesionats+START_LESIONATS.length());
				String lesionats = tr.substring(startLesionats+START_LESIONATS.length(), stopLesionats);
				try
				{
					team.setTeamPlayers(new Integer(lesionats));
				}catch(Exception e)
				{}
			}
			
			// baixes?
			int startBaixes = tr.indexOf(START_BAIXA, stopName+STOP_NAME.length());
			if( startBaixes != -1 )
				team.setStatus(Supermanager.MyTeam.STATUS_DESCARTAT);
			
			// equip incomplet
			int startIncomplet = tr.indexOf(START_INCOMPLETE, stopName+STOP_NAME.length());
			if( startIncomplet != -1 )
			{
				int stopIncomplet = tr.indexOf(STOP_INCOMPLETE, startIncomplet+START_INCOMPLETE.length());
				String incomplet = tr.substring(startIncomplet+START_INCOMPLETE.length(),stopIncomplet);
				team.setStatus(Supermanager.MyTeam.STATUS_INCOMPLETE_TEAM);
				try
				{
					team.setTeamPlayers(new Integer(incomplet));
				}catch(Exception e)
				{}
			}
			
			// day rating
			int startDayRating = tr.indexOf(START_DAY_RATING, stopName+STOP_NAME.length());
			int stopDayRating = tr.indexOf(STOP_DAY_RATING, startDayRating+START_DAY_RATING.length());
			String dayRating = tr.substring(startDayRating+START_DAY_RATING.length(), stopDayRating);
			
			// day position
			int stopDayPosition = tr.indexOf(STOP_DAY_POSITION, stopDayRating+STOP_DAY_RATING.length());
			String dayPosition = tr.substring(stopDayRating+STOP_DAY_RATING.length(), stopDayPosition);
			
			Float points = Float.valueOf("0");
			Integer pointsClass = Integer.valueOf(0);
			try{
				points = Float.valueOf(nf.parse(dayRating).floatValue());
			}catch(ParseException pe){
				points = Float.valueOf("0");
			}
			try{
				pointsClass = Integer.valueOf(nf.parse(dayPosition).intValue());
			}catch(ParseException pe){
				pointsClass = Integer.valueOf("0");
			}
			team.setPoints(points);
			team.setPointsClassification(pointsClass);
			
			// general rating
			int startGeneralRating = tr.indexOf(START_GENERAL_RATING, stopDayPosition+STOP_DAY_POSITION.length());
			int stopGeneralRating = tr.indexOf(STOP_GENERAL_RATING, startGeneralRating+START_GENERAL_RATING.length());
			String generalRating = tr.substring(startGeneralRating+START_GENERAL_RATING.length(), stopGeneralRating);
			
			// general position
			int stopGeneralPosition = tr.indexOf(STOP_GENERAL_POSITION, stopGeneralRating+STOP_GENERAL_RATING.length());
			String generalPosition = tr.substring(stopGeneralRating+STOP_GENERAL_RATING.length(), stopGeneralPosition);
			
			Float general = Float.valueOf("0");
			Integer generalClass = Integer.valueOf(0);
			try{
				general = Float.valueOf(nf.parse(generalRating).floatValue());
			}catch(ParseException pe){
				general = Float.valueOf("0");
			}
			try{
				generalClass = Integer.valueOf(generalPosition).intValue();
			}catch(Exception pe){
				generalClass = Integer.valueOf("0");
			}
		
			team.setGeneral(general);
			team.setGeneralClassification(generalClass);
			
			// broker
			int startBroker = tr.indexOf(START_BROKER_RATING, stopGeneralPosition+STOP_GENERAL_POSITION.length());
			int stopBroker = tr.indexOf(STOP_BROKER_RATING, startBroker+START_BROKER_RATING.length());
			String brokerRating = tr.substring(startBroker+START_BROKER_RATING.length(), stopBroker);
			
			// broker position
			int stopBrokerPosition = tr.indexOf(STOP_BROKER_POSITION, stopBroker+STOP_BROKER_RATING.length());
			String brokerPosition = tr.substring(stopBroker+STOP_BROKER_RATING.length(), stopBrokerPosition);
			
			Integer broker = Integer.valueOf("0");
			Integer brokerClass = Integer.valueOf(0);
			try{
				broker = Integer.valueOf(nf.parse(brokerRating).intValue());
			}catch(ParseException pe){
				broker = Integer.valueOf("0");
			}
			try{
				brokerClass = Integer.valueOf(nf.parse(brokerPosition).intValue());
			}catch(ParseException pe){
				brokerClass = Integer.valueOf("0");
			}
			team.setBroker(broker);
			team.setBrokerClassification(brokerClass);
			
			// registrem equip
			teams.add(team);
			
			// recuperem seguent startTr
			startTr = html.indexOf(START_TR, stopTr);
		}
		
		return System.nanoTime()-start;
	}
	
	public ArrayList<Team> getTeams()
	{
		return teams;
	}
	
}
